package servlet;

import entity.MetaverseProduct;
import service.MetaverseProductService;
import service.impl.MetaverseProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/servletInsert")
public class servletInsert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        MetaverseProductService service = new MetaverseProductServiceImpl();
        MetaverseProduct product = new MetaverseProduct();
        product.setProductName(request.getParameter("productName"));
        product.setProductPrice(Double.parseDouble(request.getParameter("producPrice")));
        product.setProductDesc(request.getParameter("producDesc"));
        product.setFlag(Integer.parseInt(request.getParameter("state")));
        PrintWriter out = response.getWriter();
        System.out.println(request.getContextPath()+"/mertaver");
        if (service.insert(product)){
            out.print("<script>");
            out.print("     alert('添加成功')");
            out.print("</script>");
        }else {
            out.print("<script>");
            out.print("     alert('添加失败')");
            out.print("</script>");
        }
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
